package homework;

/**
 * @Author Stringzhua
 * @Date 2024/2/4 19:22
 * description:冒泡排序
 */
public class homework07 {
    public static void main(String[] args) {
        //要求从小到大
        int[] arr = {20, -1, 89, 2, 890, 7};
        int temp = 0;//辅助交换
        for (int i = 0; i < arr.length - 1; i++) {//外层循环(轮)
            for (int j = 0; j < arr.length - 1; j++) {//每轮的比较次数
                //如果是从小到大,条件为arr[j]>arr[j+1]
                //如果是从大到小,条件为arr[j]<arr[j+1]
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        //输出排序后数组
        System.out.println("\n===排序后===");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
    }
}
